#!/bin/bash

# it should be fixed

# workaround when .indy become regular file
if [ -f /home/${SUDO_USER}/.indy ]; then
    rm /home/${SUDO_USER}/.indy
fi

GENERAL_CONFIG_DIR="/etc/indy"
GENERAL_CONFIG_NAME="indy_config.py"
GENERAL_CONFIG_PATH=$GENERAL_CONFIG_DIR/$GENERAL_CONFIG_NAME

INSTALL_DIR="/usr/local/lib/python3.5/dist-packages"

LOCAL_NETWORK='local'
SANDBOX_NETWORK='sandbox'
LIVE_NETWORK='live'
DEFAULT_NETWORK=$SANDBOX_NETWORK

ANYONE_CAN_WRITE=False


mkdir -p $GENERAL_CONFIG_DIR
mkdir -p /var/lib/indy
mkdir -p /var/log/indy

mkdir -p /home/indy/.indy-cli
mkdir -p /home/indy/.indy-cli/networks

mkdir -p /home/${SUDO_USER}/.indy-cli
mkdir -p /home/${SUDO_USER}/.indy-cli/networks

chmod -R ug+rwx /home/indy/.indy-cli
chmod -R ug+rwx /home/${SUDO_USER}/.indy-cli

for network in $LOCAL_NETWORK $SANDBOX_NETWORK $LIVE_NETWORK
do
    mkdir -p /var/lib/indy/${network}
    chmod -R ug+rwx /var/lib/indy/${network}

    mkdir -p /home/indy/.indy-cli/networks/${network}
    chmod -R ug+rwx /home/indy/.indy-cli/networks/${network}

    mkdir -p /home/${SUDO_USER}/.indy-cli/networks/${network}
    chmod -R ug+rwx /home/${SUDO_USER}/.indy-cli/networks/${network}

    cp -nr $INSTALL_DIR/sovrin/pool_transactions_${network}_genesis \
        /var/lib/indy/${network}/pool_transactions_genesis 2>/dev/null
    cp -nr $INSTALL_DIR/sovrin/pool_transactions_${network}_genesis \
        /home/indy/.indy-cli/networks/${network}/pool_transactions_genesis 2>/dev/null
    cp -nr $INSTALL_DIR/sovrin/pool_transactions_${network}_genesis \
        /home/${SUDO_USER}/.indy-cli/networks/${network}/pool_transactions_genesis 2>/dev/null

    cp -nr $INSTALL_DIR/sovrin/domain_transactions_${network}_genesis \
        /var/lib/indy/${network}/domain_transactions_genesis 2>/dev/null
    cp -nr $INSTALL_DIR/sovrin/domain_transactions_${network}_genesis \
        /home/indy/.indy-cli/networks/${network}/domain_transactions_genesis 2>/dev/null
    cp -nr $INSTALL_DIR/sovrin/domain_transactions_${network}_genesis \
        /home/${SUDO_USER}/.indy-cli/networks/${network}/domain_transactions_genesis 2>/dev/null
done


if [ ! -f $GENERAL_CONFIG_PATH ]; then
    touch $GENERAL_CONFIG_PATH
    echo "enableStdOutLogging=False" >> $GENERAL_CONFIG_PATH
    echo "LEDGER_DIR = '/var/lib/indy'" >> $GENERAL_CONFIG_PATH
    echo "LOG_DIR = '/var/log/indy'" >> $GENERAL_CONFIG_PATH
    echo "KEYS_DIR = '/var/lib/indy'" >> $GENERAL_CONFIG_PATH
    echo "GENESIS_DIR = '/var/lib/indy'" >> $GENERAL_CONFIG_PATH
    echo "BACKUP_DIR = '/var/lib/indy/backup'" >> $GENERAL_CONFIG_PATH
    echo "PLUGINS_DIR = '/var/lib/indy/plugins'" >> $GENERAL_CONFIG_PATH
    echo "CLI_BASE_DIR = '~/.indy-cli/'" >> $GENERAL_CONFIG_PATH
    echo "CLI_NETWORK_DIR = '~/.indy-cli/networks'" >> $GENERAL_CONFIG_PATH
    echo "NETWORK_NAME = '$DEFAULT_NETWORK'" >> $GENERAL_CONFIG_PATH
    echo "ANYONE_CAN_WRITE = $ANYONE_CAN_WRITE" >> $GENERAL_CONFIG_PATH
    echo "UPGRADE_ENTRY = 'sovrin'" >> $GENERAL_CONFIG_PATH
fi


NETWORK_LINE=$(grep ^NETWORK_NAME[[:space:]]*=[[:space:]]* $GENERAL_CONFIG_PATH)
if [ -z "${NETWORK_LINE}" ]; then
    echo "NETWORK_NAME = '$DEFAULT_NETWORK'" >> $GENERAL_CONFIG_PATH
elif [[ $NETWORK_LINE =~ ^NETWORK_NAME[[:space:]]*=[[:space:]]*None[[:space:]]*$ ]]; then
    sed -i "s/^\(NETWORK_NAME\s*=\s*\).*\$/\1\'$DEFAULT_NETWORK\'/" $GENERAL_CONFIG_PATH
fi


PERMISSION_LINE=$(grep ^ANYONE_CAN_WRITE[[:space:]]*=[[:space:]]* $GENERAL_CONFIG_PATH)
if [ -z "${PERMISSION_LINE}" ]; then
    echo "" >> $GENERAL_CONFIG_PATH
    echo "ANYONE_CAN_WRITE = $ANYONE_CAN_WRITE" >> $GENERAL_CONFIG_PATH
fi


TOP_LEVEL_PKT_LINE=$(grep ^UPGRADE_ENTRY[[:space:]]*=[[:space:]]* $GENERAL_CONFIG_PATH)
if [ -z "${TOP_LEVEL_PKT_LINE}" ]; then
    echo "" >> $GENERAL_CONFIG_PATH
    echo "UPGRADE_ENTRY = 'sovrin'" >> $GENERAL_CONFIG_PATH
fi

# workaround when .indy become regular file
if [ -f /home/indy/.indy ]; then
    rm /home/indy/.indy
fi


sed -ie '/HOLD_EXT/{ s/\\//g}' $GENERAL_CONFIG_DIR/node_control.conf
# adding sovrin package to packages to hold
SOVRIN_IS_HELD=$(grep HOLD_EXT=.*sovrin.* $GENERAL_CONFIG_DIR/node_control.conf)
if [ -z "${SOVRIN_IS_HELD}" ]; then
    # remove =" or just = to ="sovrin or =sovrin
    sed -ie '/HOLD_EXT=/{s/=\("\?\)/=\1sovrin /1}' $GENERAL_CONFIG_DIR/node_control.conf
fi

chown -R indy:indy $GENERAL_CONFIG_DIR
chown -R indy:indy /var/log/indy
chown -R indy:indy /var/lib/indy
chown -R indy:indy /home/indy/.indy-cli
chown -R ${SUDO_USER}:${SUDO_USER} /home/${SUDO_USER}/.indy-cli

chmod -R ug+rw $GENERAL_CONFIG_DIR
chmod -R ug+rw /var/lib/indy
chmod -R ug+rw /var/log/indy
chmod -R ug+rw /home/indy/.indy-cli
chmod -R ug+rw /home/${SUDO_USER}/.indy-cli

# Automatically added from template:
if which py3compile >/dev/null 2>&1; then
	py3compile -O -p sovrin $INSTALL_DIR/
fi

# End automatically added section
